/*
 * RecursosAction.java
 *
 * Created on 10 de diciembre de 2005, 16:36
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package org.fiel.admin.struts.actions;

import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.fiel.FielConstants;
import org.fiel.admin.dao.AreaDAO;
import org.fiel.admin.dao.CargoDAO;
import org.fiel.admin.dao.RecursoDAO;
import org.fiel.admin.struts.forms.RecursoForm;

/**
 * @author Luis Pablo Gallo (luispablo.gallo@gmail.com)
 */
public class RecursosAction extends FielAction{
    
    public ActionForward list   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        try{
            RecursoDAO dao = new RecursoDAO();
            request.setAttribute(FielConstants.RECURSOS, dao.retrieve());
        }catch (Exception ex){
            ex.printStackTrace();
            throw ex;
        }
        
        return mapping.findForward(this.authorize(request, FielConstants.SHOW_LIST));
    }
    
    public ActionForward add( ActionMapping mapping
                            , ActionForm form
                            , HttpServletRequest request
                            , HttpServletResponse response )
    throws Exception{
        request.setAttribute(FielConstants.EDITABLE, FielConstants.TRUE);
        
        this.loadFKs(request);
        
        return mapping.findForward(this.authorize(request, FielConstants.ADD));
    }

    public ActionForward save   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        RecursoDAO dao = new RecursoDAO();
        RecursoForm recurso = (RecursoForm)form;

        if (recurso.getCvFile()!=null)
        {
            if  (recurso.getCv()!=null 
                    && recurso.getCv().trim().length()>0 
                    && recurso.getId()!=null
                    && recurso.getId().trim().length()>0 )
            {
                this.deleteCV(recurso);
            }
            recurso.setCv(recurso.getCvFile().getFileName());
        }
        
        if( ((RecursoForm)form).getId()==null || ((RecursoForm)form).getId().trim().length()<=0 ){
            dao.create(recurso);
        }else{
            dao.update(recurso);
        }

        this.upload(recurso);

        return this.list(mapping, form, request, response);
    }

    public ActionForward view   ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        RecursoDAO dao = new RecursoDAO();
        RecursoForm recurso = dao.retrieve(request.getParameter("id"));
        
        ((RecursoForm)form).setId(recurso.getId());
        ((RecursoForm)form).setApellido(recurso.getApellido());
        ((RecursoForm)form).setNombre(recurso.getNombre());
        ((RecursoForm)form).setAreaId(recurso.getAreaId());
        ((RecursoForm)form).setCargoId(recurso.getCargoId());
        ((RecursoForm)form).setCv(recurso.getCv());
        
        request.setAttribute( FielConstants.EDITABLE
                            , request.getParameter(FielConstants.EDITABLE) );

        this.loadFKs(request);
        
        return mapping.findForward(this.authorize(request, FielConstants.ADD));
    }

    public ActionForward delete ( ActionMapping mapping
                                , ActionForm form
                                , HttpServletRequest request
                                , HttpServletResponse response )
    throws Exception{
        RecursoForm recurso = (RecursoForm)form;
        
        if (recurso.getId()!=null 
                && recurso.getId().trim().length()>0
                && recurso.getCv()!=null
                && recurso.getCv().trim().length()>0 )
        {
            this.deleteCV(recurso);
        }
        
        RecursoDAO dao = new RecursoDAO();
        dao.delete(request.getParameter("id"));
        
        return this.list(mapping, form, request, response);
    }
    
    private void loadFKs(HttpServletRequest request)
    throws Exception{
        AreaDAO areas = new AreaDAO();
        CargoDAO cargos = new CargoDAO();

        request.setAttribute(FielConstants.CARGOS, cargos.retrieve());
        cargos = null;
        request.setAttribute(FielConstants.AREAS, areas.retrieve());
    }
    
    private void upload(RecursoForm form)
    throws Exception{
        if( form.getCvFile()!=null ){
            String path = this.getServlet().getServletContext().getRealPath(FielConstants.CV_PATH);

            File cv = new File(path + "/" + form.getId() + "." + form.getCvFile().getFileName());
            FileOutputStream fos = new FileOutputStream(cv);
            fos.write(form.getCvFile().getFileData());
            fos.close();
        }
    }
    
    private void deleteCV(RecursoForm form)
    throws Exception
    {
        String path = this.getServlet().getServletContext().getRealPath(FielConstants.CV_PATH);
        File cvFile = new File(path + "/" + form.getId() + "." + form.getCv());
        cvFile.delete();
    }
    
}
